package xiaohang.zhimeng;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Activity01 extends ListActivity {
   
        //ListActivity一个以列表的方式显示数据源、数组的Activity
        //ListActivity Class Overview(此描述摘自官方文档说的非常清楚了)
        /*An activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.

        ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results. Binding, screen layout, and row layout are discussed in the following sections.

        Screen Layout

        ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

        Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.

        The following code demonstrates an (ugly) custom screen layout. It has a list with a green background, and an alternate red "no data" message.*/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
        HashMap<String, String> map1 = new HashMap<String, String>();
        HashMap<String, String> map2 = new HashMap<String, String>();
        HashMap<String, String> map3 = new HashMap<String, String>();
        //一个map对象对应一条数据
        map1.put("user_name", "zhangsan");
        map1.put("user_ip", "192.168.0.1");
        
        map2.put("user_name", "lisi");
        map2.put("user_ip", "192.168.0.2");
        
        map3.put("user_name", "wangwu");
        map3.put("user_ip", "192.168.0.3");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        //这里对SimpleAdapter这个构造方法的参数说明一下 E文好的直接看E文
        /*context        The context where the View associated with this SimpleAdapter is running
        data        A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
        resource        Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
        from        A list of column names that will be added to the Map associated with each item.
        to        The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.*/
        /**
         * 参数一 Context 这个不说了
         * 参数二 就是上边声明的那个ArrayList对象
         * 参数三 这个参数用来指定 我们一行数据 的key 也就是一个map对象的key 上下结合看一下 因为我们一条数据也就是一行
         * 对应一个map对象 一个map对象包含2个数据 即 user_name 和 user_ip 这个参数就是用来指定这2个key 这里是通过String数组的方式
         * 参数四  大家一看就知道了 意思是 user_name 这条数据用 R.id.user_name 这个TextView显示  user_ip 这条数据用 
         * R.id.user_ip 显示
         */
        SimpleAdapter listAdapter = new SimpleAdapter(this,list,
                        R.layout.user, new String[] {"user_name","user_ip"},
                        new int[] {R.id.user_name,R.id.user_ip});
        //这是Adapter setListAdapter()此方法来自ListActivity
        setListAdapter(listAdapter);
    }
    //当我们点击一条数据 或者说一行时 触发的Click事件
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            //我们输出它的 ID 和 position
            //ID
            System.out.println("id------------>" + id);
            //位置
            System.out.println("position--------->" + position);
    }
}